package cn.zhf.shop.service.impl;

import cn.zhf.shop.entity.Campus;
import cn.zhf.shop.entity.ConsoleUser;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.lesson.LessonType;
import cn.zhf.shop.entity.organization.OrgOverview;
import cn.zhf.shop.entity.organization.OrganizationType;
import cn.zhf.shop.entity.search.OrganizationSearch;
import cn.zhf.shop.mapper.*;
//import com.sun.javafx.scene.layout.region.SliceSequenceConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import cn.zhf.shop.entity.organization.Organization;
import cn.zhf.shop.service.OrganizationService;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Component
public class OrganizationServiceImpl implements OrganizationService {

    @Resource
    private OrganizationMapper organizationMapper;

    @Resource
    private LessonTypeMapper lessonTypeMapper;

    @Resource
    private ConsoleUserMapper consoleUserMapper;

    @Resource
    private CampusMapper campusMapper;

    @Resource
    private OrganizationTypeMapper organizationTypeMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Message<?> deleteByPrimaryKey(Integer orgId) {
        logger.info("删除机构：orgId：{}", orgId);
        List<Campus> campusList= campusMapper.selectByOrgId(orgId);
        if(campusList.size()==0){
            return Message.success(null).add(organizationMapper.deleteByPrimaryKey(orgId));
        }else{
            return  Message.fail("该条数据不能删除");
        }


    }

    @Override
    public Message insert(Organization record) {
        logger.info("添加机构：orgId：{}", record.getOrgId());
        if(organizationMapper.insert(record) > 0){
            ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(record.getHead());
            consoleUser.setState(1);
            return consoleUserMapper.updateByPrimaryKeySelective(consoleUser)>0 ? Message.success(null):Message.fail("修改负责人状态失败");
        }else {
            return Message.fail("添加失败");
        }
    }

    @Override
    public Message insertSelective(Organization record) {
        logger.info("添加机构：orgId：{}", record.getOrgId());
        return organizationMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Organization> selectByPrimaryKey(Integer orgId) {
        logger.info("查找：orgId：{}", orgId);
        Organization organization = organizationMapper.selectByPrimaryKey(orgId);
        return organization == null ? Message.fail("无记录") : Message.success(null).add(organization);
    }

    @Override
    public Message updateByPrimaryKeySelective(Organization record) {
        logger.info("更新：orgId：{}", record.getOrgId());
        if(record.getHead()==0){
            ConsoleUser consoleUser1=consoleUserMapper.selectByPrimaryKey(record.getHead());
            consoleUser1.setState(1);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser1);
        }else{
            Organization organization = organizationMapper.selectByPrimaryKey(record.getOrgId());
            ConsoleUser consoleUser=consoleUserMapper.selectByPrimaryKey(organization.getHead());
            consoleUser.setState(0);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser);
            ConsoleUser consoleUser1=consoleUserMapper.selectByPrimaryKey(record.getHead());
            consoleUser1.setState(1);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser1);
        }
        return organizationMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success("更新成功" + record.getOrgId()) : Message.fail("更新失败");
    }

    @Override
    public Message updateByPrimaryKey(Organization record) {
        logger.info("更新：orgId：{}", record.getOrgId());
        Organization organization = organizationMapper.selectByPrimaryKey(record.getOrgId());
        if(organization.getHead()==0){
            ConsoleUser consoleUser1=consoleUserMapper.selectByPrimaryKey(record.getHead());
            consoleUser1.setState(1);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser1);
        }else{
            ConsoleUser consoleUser=consoleUserMapper.selectByPrimaryKey(organization.getHead());
            consoleUser.setState(0);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser);
            ConsoleUser consoleUser1=consoleUserMapper.selectByPrimaryKey(record.getHead());
            consoleUser1.setState(1);
            consoleUserMapper.updateByPrimaryKeySelective(consoleUser1);
        }
        return organizationMapper.updateByPrimaryKey(record) > 0 ? Message.success("更新成功" + record.getOrgId()) : Message.fail("更新失败");
    }

    @Override
    public Message<List<Organization>> selectAll() {
        logger.info("获得机构列表123");
        List<Organization> refund = organizationMapper.selectAll();
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Organization>> selectByOrgName(String orgName) {
        logger.info("获得机构列表ByName");
        List<Organization> refund = organizationMapper.selectByOrgName(orgName);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Organization>> selectByOrgType(String orgType) {
        logger.info("获得机构列表ByType");
        List<Organization> refund = organizationMapper.selectByOrgType(orgType);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Organization>> selectByHead(int head) {
        logger.info("获得机构列表ByHead");
        OrgOverview refund = organizationMapper.selectByHead3(head);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Organization selectByPrimaryKey2(Integer orgId) {
        return organizationMapper.selectByPrimaryKey(orgId);
    }

    @Override
    public List<Organization> selectAll2() {
        return organizationMapper.selectAll();
    }

    @Override
    public List<Organization> selectByReferenceId(Integer referenceId) {
        return organizationMapper.selectByReferenceId(referenceId);
    }

    public Message<List<Organization>> selectByReferenceId1(Integer referenceId) {
        List<Organization> organizationList = organizationMapper.selectByReferenceId(referenceId);
        for (Organization organization:organizationList){
            organization.setTypeName(lessonTypeMapper.selectTypeNameById(organization.getOrgType()));
        }
        return Message.success(null).add(organizationList);
    }

    @Override
    public Message<List<Organization>> selectSearch1(String orgName,Integer orgType){

        List<Organization> organizationList = organizationMapper.selectBySearch1(orgName,orgType);
        List<Organization> organizationList1 = new LinkedList<>();
        for (int i=0;i<organizationList.size();i++)
        {
            Organization organization = organizationList.get(i);
            organization.setTypeName(lessonTypeMapper.selectByPrimaryKey(organization.getOrgType()).getTypeName());
            ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(organization.getHead());
            organization.setOrg_username(consoleUser.getUsername());
            organizationList1.add(organization);
        }
        return Message.success(null).add(organizationList1);
    }

    @Override
    public Message<List<Organization>> selectSearch2(String str){
        List<Organization> organizationList = organizationMapper.selectBySearch2(str);
        List<Organization> organizationList1 = new LinkedList<>();
        for (int i=0;i<organizationList.size();i++) {
            Organization organization = organizationList.get(i);
            organization.setTypeName(lessonTypeMapper.selectByPrimaryKey(organization.getOrgType()).getTypeName());
            ConsoleUser consoleUser = consoleUserMapper.selectByPrimaryKey(organization.getHead());
            organization.setOrg_username(consoleUser.getUsername());
            organizationList1.add(organization);
        }
        return Message.success(null).add(organizationList1);
    }

    @Override
    public Message mohuConditionSearch(OrganizationSearch c) {
        List<Organization> organizationList =organizationMapper.selectByOrgNameAndOrgTypeAndContactsAndHeadName(c);
        for (Organization organization:organizationList){
            organization.setTypeName(lessonTypeMapper.selectTypeNameById(organization.getOrgType()));

        }

        return Message.success(null).add(organizationList);
    }


}

